package top.jamewen.framework.sbmp.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.jamewen.framework.sbmp.common.api.CommonResult;
import top.jamewen.framework.sbmp.common.model.UmsResourceCategory;
import top.jamewen.framework.sbmp.service.UmsResourceCategoryService;

import java.util.List;

/**
 * <p>
 * 资源分类表 前端控制器
 * </p>
 *
 * @author jamewen
 * @since 2021-01-12
 */
@Api(tags = "后台资源分类管理模块")
@RestController
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {
    @Autowired
    private UmsResourceCategoryService resourceCategoryService;

    @ApiOperation("查询所有后台资源分类")
    @RequestMapping(method = RequestMethod.GET)
    public CommonResult<List<UmsResourceCategory>> listAll() {
        List<UmsResourceCategory> resourceList = resourceCategoryService.listAll();
        return CommonResult.success(resourceList);
    }

    @ApiOperation("添加后台资源分类")
    @RequestMapping(method = RequestMethod.POST)
    public CommonResult<String> create(@RequestBody UmsResourceCategory umsResourceCategory) {
        boolean success = resourceCategoryService.create(umsResourceCategory);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台资源分类")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public CommonResult<String> update(@PathVariable Long id, @RequestBody UmsResourceCategory umsResourceCategory) {
        umsResourceCategory.setId(id);
        boolean success = resourceCategoryService.updateById(umsResourceCategory);
        if (success) {
            return CommonResult.success("修改后台资源分类成功");
        } else {
            return CommonResult.failed("修改后台资源分类失败");
        }
    }

    @ApiOperation("删除后台资源")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<String> delete(@PathVariable Long id) {
        boolean success = resourceCategoryService.removeById(id);
        if (success) {
            return CommonResult.success("删除后台资源成功");
        } else {
            return CommonResult.failed("删除后台资源失败");
        }
    }
}

